﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using XclusiveService.Models;

namespace XclusiveService.Controllers
{
    public class EmployeeController : ApiController
    {
        static readonly IEmployeeRepository repository = new EmployeeRepository();

        //Getting all the Employees
        public IEnumerable<Employee> GetAllEmployees()
        {
            return repository.GetAllEmployees();
        }

        //Getting the Employees by company_name
        public IEnumerable<Employee> GetEmployeesByCompanyName(string company_name)
        {
            return repository.GetEmployeesByCompanyName(company_name);
        }

        //Getting an Employee by employeeID
        public Employee GetEmployeeByEmployeeID(int employeeID)
        {
            Employee emp = repository.GetEmployeeByEmployeeID(employeeID);

            if (emp == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return emp;
        }

        //Creating an Employee (POST)
        public HttpResponseMessage CreateEmployee(Employee emp)
        {
            emp = repository.CreateEmployee(emp);
            var response = Request.CreateResponse<Employee>(HttpStatusCode.Created, emp);

            string uri = Url.Link("DefaultApi", new { employeeID = emp.employeeID });
            response.Headers.Location = new Uri(uri);

            return response;
        }

        //Updating an Employee (PUT)
        public void UpdateEmployee(int employeeID, Employee emp)
        {
            emp.employeeID = employeeID;

            if (!repository.UpdateEmployee(emp))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        //Deleting an Employee by EmployeeID
        public HttpResponseMessage DeleteEmployeeByEmployeeID(int employeeID)
        {
            repository.DeleteEmployeeByEmployeeID(employeeID);

            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }

        //Deleting the employees by company_name
        public HttpResponseMessage DeleteEmployeeByCompanyName(int company_name)
        {
            repository.DeleteEmployeeByCompanyName(company_name);

            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
    }
}
